<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Info extends CI_Controller {
    private $data = array();

    public function  __construct() {
        parent::__construct();
        //Load Helper
        $this->load->helper("url");
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->config->set_item('compress_output', TRUE);
        
         //Load Libary
        $this->load->library("ci_smarty");

        // Load Model
        $this->load->Model("Mcategory");
        $this->load->Model("Minfo");
        $this->load->Model("Mbook");
        $this->load->Model("MPromotion");
        $this->load->Model("MSeo");
        
        if(WEB_TYPE == 'T'){
            $this->data['dataAll'] = $this->Mcategory->getCategoriesByType('T');
        }else {
            $this->data['dataAll'] = $this->Mcategory->getCategoriesByType('C');
        }
        
        //Load Data
        $this->data['infoAll'] = $this->Minfo->getInfo();
        $this->data['bookBestList'] = $this->Mbook->getBookPopularBySumViewChapter();
        $this->data['promotion1'] = $this->MPromotion->getPromotionByPos(1);
        $this->data['promotion2'] = $this->MPromotion->getPromotionByPos(2);
        $this->data['seoData'] = $this->MSeo->getCommonSEO(); //Luôn có ở tất cả controller
    }

    public function detailInfo($uname)
    {
        $this->data['top_menu_selected'] = $uname;
        $info = $this->Minfo->getInfobyUname($uname);
        $this->data['info'] = $info;
        
        // seo info template
        $categoryLink = $this->data['base_url'] . 'thong-tin/' . $info['uname'] . '.html';
        $title = $info['page_title'];
        $keyword= $info['page_keyword'];
        $description = $info['page_description'];
        
        $this->data['seoData']['title'] = $title;
        $this->data['seoData']['description'] = $description;
        $this->data['seoData']['news_keywords'] = $keyword;
        $this->data['seoData']['canonical'] = $categoryLink;
 
        $this->data['seoData']['og:url'] = $categoryLink;
        $this->data['seoData']['og:title'] = $title;
        $this->data['seoData']['description'] = $description; 
        
        $this->data['seoTemplate'] = $this->MSeo->getTemplateSEO($this->data['seoData']);
        if($info['id'] == 1){
            $this->ci_smarty->view('frontend/info.tpl', $this->data); 
        }else {
            $this->ci_smarty->view('frontend/contact.tpl', $this->data); 
        }
    }
    
    public function sendMail()
    {
        $config = array(
                'protocol' => 'smtp',
                'smtp_host' => 'ssl://smtp.googlemail.com',
                'smtp_port' => '465',
                'smtp_user' => 'truyen.thanhphogame@gmail.com',
                'smtp_pass' => 'istarweb89'//Nhớ đánh đúng user và pass nhé
        );
        $this->load->library('email',$config);
        $this->email->set_newline("\r\n");
        
        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $subject = $this->input->post('subject');
        $comments = $this->input->post('comments');
        
        $this->email->from($email, $name);
        $this->email->to('truyen.thanhphogame@gmail.com');    
        $this->email->subject($subject);
        $this->email->message($comments);    
        
        if($this->email->send()){
            echo "Mail đã được gửi cho bạn";
            redirect(base_url()."thong-tin/lien-he.html"); 
        }else{
            show_error($this->email->print_debugger());
        }
    }

}